<?php
if( !function_exists('tpl_parse_ext')) {
	function tpl_parse_ext($template){
		return template_ext($template);
	}
}
//模板扩展函数
function template_ext($template)
{
		$template = preg_replace ('/\{\\$url\((.*)\)\}/iU', '<?php echo url("$1");  ?>', $template);

		//替换文件路径
	    $template = preg_replace("/<(.*?)(src=|href=|value=|background=)[\"|\'](images\/|img\/|css\/|js\/|style\/)(.*?)[\"|\'](.*?)>/i",
	        "<$1$2\"".__ROOT__."/<?php echo \$config['TPL']['TPL_TEMPLATE_PATH'].\$config['TPL']['TPL_TEMPLATE_NAME']; ?>/"."$3$4\"$5>", $template);

	    //替换载入模板
	    $template = preg_replace('/<!--#include\s*file=[\"|\'](.*)[\"|\']-->/iU',
	                    "<?php \$file=explode('.', \"$1\"); ?>{include file=\"\$file[0]\"}", $template);

	    //替换菜单与内容反向超链接
	    
	    $template = preg_replace("/\{\\\$curl\.([a-z0-9_]+)\}/iU",
	        "<?php echo api('content','category_curl',$1); ?>", $template);
	    $template = preg_replace("/\{\\\$aurl\.([a-z0-9_]+)\}/iU",
	        "<?php echo api('content','category_aurl',$1); ?>", $template);
		
		$template = preg_replace("/\{\\$([a-z0-9_]+)\.furl\}/iU",
	        "<?php  echo api('default','label_getfurl',$1); ?>", $template);

		$template = preg_replace("/\{\\\$my\.([a-z0-9_]+)\}/iU",
	        "<?php \$ad_$1=api('advertising','info_label',$1); if(!is_array(\$ad_$1)){ echo \$ad_$1; } ?>", $template);
	    

		//PHP语句
	    /*{$php(xxx)}替换成 <?php xxx ?>*/
	    $template = preg_replace ('/\{\\$php\((.+?)\)\}/iU', '<?php $1 ?>', $template);


	    //添加循环解析标签
	    /*<!--foreach:{$arr $vo}-->替换成 <?php $vo_i=1; if (is_array($arr) foreach($arr as $vo){ ?>*/
	    $template = preg_replace ('/<!--foreach:{(\S+)\s+(\S+)}-->\s*/i','<?php if(is_array($1)) foreach($1 AS $2) {  ?>', $template);
	    /*<!--foreach:{$arr $key $vo}-->替换成 <?php $vo_i=1; if (is_array($arr) foreach($arr as $key => $vo){ ?>*/
	    $template = preg_replace ('/<!--foreach:{(\S+)\s+(\S+)\s+(\S+)}-->\s*/i','<?php  if(is_array($1)) foreach($1 AS $2 => $3) {  ?>', $template);
	    /*<!--{/foreach}-->替换成 <?php } ?>*/
	    $template = preg_replace ('/<!--\{\/foreach\}-->\s*/iU', '<?php } ?>', $template);

	    //添加判断标签
	    /*<!--if:{}-->替换成 <?php if(condition){ ?>*/
	    $template = preg_replace ('/<!--\if:{(.+?)\}-->/i', '<?php if($1) { ?>', $template);
	    /*<!--elseif:{}-->替换成 <?php }else if(condition){ ?>*/
	    $template = preg_replace ('/<!--\elseif:{(.+?)\}-->/i', '<?php }else if($1) { ?>', $template);
	    /*<!--{else}-->替换成 <?php }else{ ?>*/
	    $template = preg_replace ('/<!--\{else\}-->/i', '<?php }else{ ?>', $template);
	    /*<!--{/if}-->替换成 <?php } ?>*/
	    $template = preg_replace ('/<!--\/if-->/i', '<?php } ?>', $template);
	    $template = preg_replace ('/<!--\{\/if\}-->/i', '<?php } ?>', $template);

	    //添加打印标签
	    $template = preg_replace ('/<!--dump\{(.+?)\}-->/iU', '<?php print_r($1) ?>', $template);

	    //HTML输出
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+html\}/iU", "<?php echo html_out($1['$2']); ?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\s+html\}/iU", "<?php echo html_out($1[\'$2\'][\'$3\']); ?>", $template);

	    //时间标签
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+time\=[\"|\'](.*)[\"|\']\}/iU", "<?php echo date('$3',$1['$2']); ?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\s+time\=[\"|\'](.*)[\"|\']\}/iU", "<?php echo date('$4',$1[\'$2\'][\'$3\']); ?>", $template);

	    //图片裁剪
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+cutout\=[\"|\'](.*)[\"|\']\}/iU", "<?php echo module('label')->get_cutout($1['$2'],'$3'); ?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\s+cutout\=[\"|\'](.*)[\"|\']\}/iU", "<?php echo module('label')->get_cutout($1[\'$2\'][\'$3\'],'$4'); ?>", $template);
	    
	    //为空默认值
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+empty\=[\"|\'](.*)[\"|\']\}/iU", "<?php if(!empty($1['$2'])){echo $1['$2'];}else{echo '$3';}?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\s+empty\=\"(.*)\"\}/iU", "<?php if(!empty($1[\'$2\'][\'$3\'])){echo $1[\'$2\'][\'$3\'];}else{echo '$4';}?>", $template);

	    //不存在默认值
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+isset\=[\"|\'](.*)[\"|\']\}/iU", "<?php if(isset($1['$2'])){echo $1['$2'];}else{echo '$3';}?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\s+isset\=\"(.*)\"\}/iU", "<?php if(isset($1[\'$2\'][\'$3\'])){echo $1[\'$2\'][\'$3\'];}else{echo '$4';}?>", $template);

	    //TAG标签
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+tag\}/iU", "<?php \$tag=explode(',', $1['$2']); foreach (\$tag as \$value) { \$tags.= '<a href=\"'.api('default','label_getturl',\$value).'\" title=\"'.\$value.'\">'.\$value.'</a>,'; } echo substr(\$tags, 0,-1); unset(\$tags); ?>", $template); 

	    //标题样式
	    $template = preg_replace("/\{(\\$[a-z0-9_]+)\.titlex\}/iU", "<?php \$titlex=$1['title']; if(!empty($1['font_color'])){ \$titlex='<font color=\"'.$1['font_color'].'\">'.\$titlex.'</font>'; } if($1['font_bold']==1){ \$titlex='<strong>'.\$titlex.'</strong>'; }  echo \$titlex;  unset(\$titlex); ?>",
	        $template);

	    //样式+字数
	    $template = preg_replace("/\{(\\$[a-z0-9_]+)\.titlex\s+len\=[\"|\'](.*)[\"|\']\}/iU", "<?php \$titlex=msubstr($1['title'],0,$2); if(!empty($1['font_color'])){ \$titlex='<font color=\"'.$1['font_color'].'\">'.\$titlex.'</font>'; } if($1['font_bold']==1){ \$titlex='<strong>'.\$titlex.'</strong>'; }  echo \$titlex; unset(\$titlex); ?>",
	        $template);

	    //字符截取
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\s+len\=[\"|\'](.*)[\"|\']\}/iU", "<?php echo msubstr($1['$2'],0,$3); ?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\s+len\=[\"|\'](.*)[\"|\']\}/iU", "<?php echo msubstr($1[\'$2\'][\'$3\'],0,$4); ?>", $template);
		
		//数组标签
		/*{$info.xxx}替换成 <?php echo $info['xxx'] ?>*/
		$template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\}/i", "<?php echo $1['$2']; ?>", $template);
	    $template = preg_replace ( "/\{(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\}/i", "<?php echo $1[\'$2\'][\'$3\']; ?>", $template);

	    //转义循环  
	    $test=preg_match_all('/<!--(\S+):{(.*)}-->/i',$template,$ms);
	    foreach ($ms[2] as $value) {
	        $template=str_replace($value, addslashes($value), $template);
	    }

	    //处理循环内条件标签
	    $template = preg_replace ( "/\<(\\$[a-z0-9_]+)\.([a-z0-9_]+)\>/i", "'.$1['$2'].'", $template);
	    $template = preg_replace ( "/\<(\\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)>/i", "'.$1['$2']['$3'].'", $template);

	    //数据统计循环
	    $template = preg_replace('/<!--count:{(.*)}-->/i', '<?php echo api("default","label_getcount",\'$1\');  ?> ',
	        $template);

	    //替换通用循环
	    $template = preg_replace('/<!--(\S+):{(.*)}-->\s*/i', '<?php $$1=api("default","label_getlist",\'$2\'); if(is_array($$1)) foreach($$1 as $$1){ ?> ',
	        $template);

	    //循环结束标识
	    $template = preg_replace('/<!--\/([a-zA-Z_]+)-->/i', '<?php }unset($$1) ?>', $template);
	    $template = preg_replace('/<!--\{\/([a-zA-Z_]+)\}-->/i', '<?php }unset($$1) ?>', $template);

		return $template;

}
?>